<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/15
 **/

namespace app\admin\validate;


use app\common\exception\ServiceException;
use app\common\model\AdminModel;
use think\Validate;

class AdminValidate extends Validate
{
    const SCENE_USER_SETTING = 'user_setting';

    const SCENE_SAVE = 'save';

    const SCENE_PASSWORD = 'password';

    protected $rule = [
        'id|ID' => 'checkId',
        'username|登陆账号' => 'max:20|checkUserName',
        'mobile|手机号' => 'mobile',
        'email|邮箱' => 'email',
        'remark|备注' => 'max:60',
        'password|密码' => 'require'
    ];

    protected $scene = [
        'user_setting' => ['mobile', 'email', 'remark'],
        'save' => ['id', 'mobile', 'email', 'remark', 'username'],
        'password' => ['password'],
    ];

    protected function checkId($value, $rule, $data = [])
    {
        if (isset($data['id'])) { //是修改
            if ($value == 1) {
                return "超级管理员账号不允许编辑";
            }
        }
        return true;
    }

    protected function checkUserName($value, $rule, $data = [])
    {
        if (AdminModel::field("id")->where(["username" => $value])->find()) {
            return '不允许添加相同账号';
        }
        return true;
    }


}